﻿using System.Collections.Generic;
using System.Data;
using System;



namespace ExtensionsLib.Dataset
{

    public static class DataTableExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        public static IDictionary<string, string> ToDictionary(this DataTable dt, int row)
        {
            if (dt.Rows.Count <= row)
            {
                throw new Exception("The rowindex to fetch (" + row + ") must be lower than the rowcount " + dt.Rows.Count + " !");
            }
            return dt.Rows[row].ToDictionary();
        }
           


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static IList<IDictionary<string, string>> ToDictionaryList(this DataTable dt)
        {
            IList<IDictionary<string, string>> listOfDictionaries = new List<IDictionary<string, string>>();
            foreach (DataRow row in dt.Rows)
            {
                listOfDictionaries.Add(row.ToDictionary());
            }
            return listOfDictionaries;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSet"></param>
        /// <returns></returns>
        public static bool IsEmpty(this DataSet dataSet)
        {
            bool isEmpty = false;
            if (dataSet == null)
            {
                isEmpty = true;
            }
            else
            {
                if (dataSet.Tables.Count == 0)
                {
                    isEmpty = true;
                }
                else
                {
                    isEmpty = dataSet.Tables[0].Rows.Count == 0 ? true : false;
                }
            }
            return isEmpty;
        }



    }
}
